package tv.huan.tencent.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.tencent.domain.CardPool;
import tv.huan.tencent.mapper.CardBuyLogMapper;
import tv.huan.tencent.domain.CardBuyLog;
import tv.huan.tencent.mapper.CardPoolMapper;
import tv.huan.tencent.service.ICardBuyLogService;

/**
 * 购买记录Service业务层处理
 * 
 * @author admin
 * @date 2020-12-23
 */
@Service
public class CardBuyLogServiceImpl implements ICardBuyLogService 
{
    @Autowired
    private CardBuyLogMapper cardBuyLogMapper;

    @Autowired
    private CardPoolMapper cardPoolMapper;

    /**
     * 查询购买记录
     * 
     * @param id 购买记录ID
     * @return 购买记录
     */
    @Override
    public CardBuyLog selectCardBuyLogById(Integer id)
    {
        return cardBuyLogMapper.selectCardBuyLogById(id);
    }

    /**
     * 查询购买记录列表
     * 
     * @param cardBuyLog 购买记录
     * @return 购买记录
     */
    @Override
    public List<CardBuyLog> selectCardBuyLogList(CardBuyLog cardBuyLog)
    {
        return cardBuyLogMapper.selectCardBuyLogList(cardBuyLog);
    }

    /**
     * 新增购买记录
     * 
     * @param cardBuyLog 购买记录
     * @return 结果
     */
    @Override
    public int insertCardBuyLog(CardBuyLog cardBuyLog)
    {
        updateCardPoolCount(cardBuyLog);
        cardBuyLog.setCreateTime(DateUtils.getNowDate());
        return cardBuyLogMapper.insertCardBuyLog(cardBuyLog);
    }

   //新增、修改购买记录时更新卡池数量
    private void updateCardPoolCount(CardBuyLog cardBuyLog){
        CardPool cp = cardPoolMapper.selectCardPoolById(cardBuyLog.getCardId());
        //添加
        if (null == cardBuyLog.getId()){
            cp.setTotal(cp.getTotal() + cardBuyLog.getBuyCount());
            cp.setPredictRemainder(cp.getPredictRemainder() + cardBuyLog.getPredictRemainder());
            cp.setActualRemainder(cp.getActualRemainder() + cardBuyLog.getActualRemainder());
        }else {
            //编辑
            CardBuyLog exist = cardBuyLogMapper.selectCardBuyLogById(cardBuyLog.getId());
            int buyCountDiffer = cardBuyLog.getBuyCount() - exist.getBuyCount();
            int predictRemaindeDiffer = cardBuyLog.getPredictRemainder() - exist.getPredictRemainder();
            int ActualRemainderDiffer = cardBuyLog.getActualRemainder() - exist.getActualRemainder();
            cp.setTotal(cp.getTotal() + buyCountDiffer);
            cp.setPredictRemainder(cp.getPredictRemainder() + predictRemaindeDiffer);
            cp.setActualRemainder(cp.getActualRemainder() + ActualRemainderDiffer);
        }
        cardPoolMapper.updateCardPool(cp);
    }

    /**
     * 修改购买记录
     * 
     * @param cardBuyLog 购买记录
     * @return 结果
     */
    @Override
    public int updateCardBuyLog(CardBuyLog cardBuyLog)
    {
        updateCardPoolCount(cardBuyLog);
        cardBuyLog.setUpdateTime(DateUtils.getNowDate());
        return cardBuyLogMapper.updateCardBuyLog(cardBuyLog);
    }

    /**
     * 删除购买记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCardBuyLogByIds(String ids)
    {
        String[] idArr = Convert.toStrArray(ids);
        CardBuyLog cardBuyLog;
        for (String id : idArr){
            cardBuyLog = cardBuyLogMapper.selectCardBuyLogById(Integer.parseInt(id));
            if (cardBuyLog.getPredictRemainder().intValue() != cardBuyLog.getBuyCount().intValue()){
                return -1;
            }
        }
        CardPool cardPool;
        for (String id : idArr){
            cardBuyLog = cardBuyLogMapper.selectCardBuyLogById(Integer.parseInt(id));
            cardPool = cardPoolMapper.selectCardPoolById(cardBuyLog.getCardId());
            cardPool.setTotal(cardPool.getTotal() - cardBuyLog.getBuyCount());
            cardPool.setPredictRemainder(cardPool.getPredictRemainder() - cardBuyLog.getPredictRemainder());
            cardPool.setActualRemainder(cardPool.getActualRemainder() - cardBuyLog.getActualRemainder());
            cardPoolMapper.updateCardPool(cardPool);
        }
        return cardBuyLogMapper.deleteCardBuyLogByIds(idArr);
    }

    /**
     * 删除购买记录信息
     * 
     * @param id 购买记录ID
     * @return 结果
     */
    public int deleteCardBuyLogById(Integer id)
    {
        return cardBuyLogMapper.deleteCardBuyLogById(id);
    }
}
